home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Science / µSim 1.0b5 folder / source / SimUtils.c < prev    next >
Encoding:
Text File  |  1994-10-04  |  9.5 KB  |  354 lines  |  [TEXT/MMCC]

  1. /*
  2. Copyright © 1993,1994 by Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware: you can copy, exchange, modify this
  5. code as you wish. You may include this code in any kind of application: freeware,
  6. shareware, or commercial, provided that full credits are given.
  7. You may not sell or distribute this code for profit.
  8. */
  9.  
  10. //#pragma load "MacDump"
  11.  
  12. #include    "UtilsSys7.h"
  13. #include    "Globals.h"
  14. #include    "Animation.h"
  15. #include    "ControlStore.h"
  16. #include    "Dump.h"
  17. #include    "Disasm.h"
  18. #include    "Input.h"
  19. #include    "Main.h"
  20. #include    "Preferences.h"
  21. #include    "Registers.h"
  22. #include    "SimUtils.h"
  23. #include    "Conversions.h"
  24.  
  25. #if defined(FabSystem7orlater)
  26.  
  27. #define    LOWSHORTADDR(l)    (((short *)(&l))+1)
  28.  
  29. #pragma segment __%Main
  30.  
  31.  
  32. /* FromStringToNum: common code for translating a string with a specified
  33. format to a number */
  34.  
  35. void FromStringToNum(ConstStr255Param sourceStr, long *destN, short whichFormat)
  36. {
  37.  
  38. switch( whichFormat ) {
  39.     case kPOP_DEC:    StringToNum(sourceStr, destN);
  40.                     break;
  41.     case kPOP_HEX:    HexStringToShort(sourceStr, LOWSHORTADDR(*destN));
  42.                     break;
  43.     case kPOP_OCT:    OctStringToShort(sourceStr, LOWSHORTADDR(*destN));
  44.                     break;
  45.     case kPOP_BIN:    BinStringToShort(sourceStr, LOWSHORTADDR(*destN));
  46.                     break;
  47.     case kPOP_TEXT:    *(char *)LOWSHORTADDR(*destN) = sourceStr[1];
  48.                     *((char *)LOWSHORTADDR(*destN) + 1) = sourceStr[2];
  49.                     break;
  50.     }
  51. }
  52.  
  53. /* FromNumToString: common code for translating a number into a
  54. string with a specified format */
  55.  
  56. void FromNumToString(Str255 destStr, short sourceN, short whichFormat)
  57. {
  58.  
  59. switch( whichFormat ) {
  60.     case kPOP_DEC:    MyNumToString(sourceN, destStr);
  61.                     break;
  62.     case kPOP_HEX:    ShortToHexString(sourceN, destStr);
  63.                     break;
  64.     case kPOP_OCT:    ShortToOctString(sourceN, destStr);
  65.                     break;
  66.     case kPOP_BIN:    ShortToBinString(sourceN, destStr);
  67.                     break;
  68.     case kPOP_TEXT:    destStr[0] = 2;
  69.                     destStr[1] = *((char *)&sourceN);
  70.                     destStr[2] = *((char *)&sourceN + 1);
  71.                     break;
  72.     }
  73. }
  74.  
  75. /* MyGrowZone: the Grow Zone proc! */
  76.  
  77. pascal long MyGrowZone(Size /*qNeeded*/)
  78. {
  79. //#pragma unused    (qNeeded)
  80.  
  81. #ifndef __SCRIPT__
  82. #define    smSystemScript    -1
  83. #endif
  84.  
  85. ParamBlockRec    myPB;
  86. Str63    tempFName;
  87. EventRecord    dummyEv;
  88. FSSpec    thisFSSpec;
  89. short    state[kNUMOFSTATEPARTS];    /* the registers & editable parts of my machine */
  90. register Handle    dangerousHandle = GZSaveHnd();
  91. register Handle    readyBuffer;
  92. register long    tempA5;
  93. long    thisDirID;
  94. short    thisVRefNum, tmpFRefN;
  95. register OSErr    err;
  96. register Boolean    somethingInTheTrash = false;
  97.  
  98. tempA5 = SetCurrentA5();
  99. InitCursor();
  100. if (gWPtr_Animation)
  101.     if (offScr) {
  102.         if (offScr->portBits.baseAddr)
  103.             DisposePtr(offScr->portBits.baseAddr);
  104.         if (offScr->visRgn && offScr->clipRgn)
  105.             ClosePort(offScr);
  106.         DisposePtr((Ptr)offScr);
  107.         }
  108. if (gWPtr_Animation &&
  109.     gWPtr_Registers &&
  110.     Ctrl_Base &&
  111.     gWPtr_IO &&
  112.     gWPtr_Microprogram_Ed &&
  113.     gWPtr_Dump &&
  114.     dumpVScroll &&
  115.     gWPtr_Disasm &&
  116.     disasmVScroll)
  117.         SavePreferencesFile();
  118. if (gWPtr_Disasm) {
  119.     DisposeWindow(gWPtr_Disasm);
  120.     gWPtr_Disasm = nil;
  121.     }
  122. if (gWPtr_Dump) {
  123.     DisposeWindow(gWPtr_Dump);
  124.     gWPtr_Dump = nil;
  125.     }
  126. if (gWPtr_IO) {
  127.     if (((DocumentPeek)gWPtr_IO)->docTE != (TEHandle)dangerousHandle)
  128.         if (((DocumentPeek)gWPtr_IO)->docTE) {
  129.             TEDispose(((DocumentPeek)gWPtr_IO)->docTE);
  130.             ((DocumentPeek)gWPtr_IO)->docTE = nil;
  131.             }
  132.     DisposeWindow(gWPtr_IO);
  133.     gWPtr_IO = nil;
  134.     }
  135. if (gWPtr_Registers) {
  136.     DisposeWindow(gWPtr_Registers);
  137.     gWPtr_Registers = nil;
  138.     }
  139. if (gWPtr_Animation) {
  140.     DisposeWindow(gWPtr_Animation);
  141.     gWPtr_Animation = nil;
  142.     }
  143. /* we should have freed some memory */
  144. if (gMMemory) {
  145.     if ((err = FindFolder(kOnSystemDisk, kTemporaryFolderType, kCreateFolder,
  146.                             &thisVRefNum, &thisDirID)) == noErr) {
  147.         MyNumToString(TickCount(), tempFName);
  148.         (void)FSMakeFSSpec(thisVRefNum, thisDirID, tempFName, &thisFSSpec);
  149.         if ((err = FSpCreate(&thisFSSpec, kFCR_MINE, kFTY_RAM,
  150.                             smSystemScript)) == noErr)
  151.             if ((err = FSpOpenDF(&thisFSSpec, fsWrPerm, &tmpFRefN)) == noErr) {
  152.                 myPB.ioParam.ioCompletion = nil;
  153.                 myPB.ioParam.ioBuffer = gMMemory;
  154.                 myPB.ioParam.ioReqCount = kSIZE_RAM;
  155.                 myPB.ioParam.ioPosMode = fsFromStart;
  156.                 myPB.ioParam.ioRefNum = tmpFRefN;
  157.                 myPB.ioParam.ioPosOffset = 0L;
  158.                 (void)PBWriteAsync(&myPB);    // in temporary file
  159.                 while (myPB.ioParam.ioResult > 0) {
  160.                     SystemTask();
  161.                     (void)EventAvail(everyEvent, &dummyEv);
  162.                     }
  163.                 (void)FSClose(tmpFRefN);
  164.                 DisposePtr(gMMemory);
  165.                 gMMemory = nil;
  166.                 if ((err = myPB.ioParam.ioResult) == noErr) {
  167.                     somethingInTheTrash = true;
  168.                     err = AddSTRRes2Doc(&thisFSSpec, kFCR_MINE, kFTY_RAM,
  169.                                         kSTR_ApplicationName, smSystemScript);
  170.                     }
  171.                 }
  172.         MyNumToString(TickCount() + 1, tempFName);
  173.         (void)FSMakeFSSpec(thisVRefNum, thisDirID, tempFName, &thisFSSpec);
  174.         if ((err = FSpCreate(&thisFSSpec, kFCR_MINE, kFTY_REG,
  175.                             smSystemScript)) == noErr)
  176.             if ((err = FSpOpenDF(&thisFSSpec, fsWrPerm, &tmpFRefN)) == noErr) {
  177.                 BlockMoveData((Ptr)&gParts, (Ptr)&state, sizeof(gParts));
  178.                 BlockMoveData((Ptr)&gRegs, (Ptr)(state + kNUMOFPARTS), sizeof(state) - sizeof(gParts));
  179.                 myPB.ioParam.ioCompletion = nil;
  180.                 myPB.ioParam.ioBuffer = (Ptr)&state;
  181.                 myPB.ioParam.ioReqCount = sizeof(state);
  182.                 myPB.ioParam.ioPosMode = fsFromStart;
  183.                 myPB.ioParam.ioRefNum = tmpFRefN;
  184.                 myPB.ioParam.ioPosOffset = 0L;
  185.                 (void)PBWriteAsync(&myPB);    // in temporary file
  186.                 while (myPB.ioParam.ioResult > 0) {
  187.                     SystemTask();
  188.                     (void)EventAvail(everyEvent, &dummyEv);
  189.                     }
  190.                 (void)FSClose(tmpFRefN);
  191.                 if ((err = myPB.ioParam.ioResult) == noErr) {
  192.                     somethingInTheTrash = true;
  193.                     err = AddSTRRes2Doc(&thisFSSpec, kFCR_MINE, kFTY_REG,
  194.                                         kSTR_ApplicationName, smSystemScript);
  195.                     }
  196.                 }
  197.         readyBuffer = PrepareBufferFromList();
  198.         UnloadSeg(PrepareBufferFromList);
  199.         if (readyBuffer) {
  200.             MyNumToString(TickCount() + 2, tempFName);
  201.             (void)FSMakeFSSpec(thisVRefNum, thisDirID, (ConstStr255Param)&tempFName, &thisFSSpec);
  202.             if ((err = FSpCreate(&thisFSSpec, kFCR_MINE, kFTY_CSTORE,
  203.                                 smSystemScript)) == noErr)
  204.                 if ((err = FSpOpenDF(&thisFSSpec, fsWrPerm, &tmpFRefN)) == noErr) {
  205.                     err = WriteMicroprogramData(readyBuffer, &thisFSSpec, tmpFRefN,
  206.                                                 smSystemScript);
  207.                     UnloadSeg(WriteMicroprogramData);
  208.                     (void)FSClose(tmpFRefN);
  209.                     if (err == noErr)
  210.                         err = AddSTRRes2Doc(&thisFSSpec, kFCR_MINE, kFTY_CSTORE,
  211.                                             kSTR_ApplicationName, smSystemScript);
  212.                     }
  213.             }
  214.         }
  215.     }
  216. if (gMMemory) {
  217.     DisposePtr(gMMemory);
  218.     gMMemory = nil;
  219.     }
  220. if (somethingInTheTrash)
  221.     (void)StopAlert(kALRT_MEMTOOFULL, nil);
  222. tempA5 = SetA5(tempA5);
  223. ExitToShell();
  224. }
  225.  
  226. /* SetupVertScrollBar: adjusts the vertical scrollbar to the right border
  227. of the window */
  228.  
  229. void SetupVertScrollBar(WindowPtr wind, ControlHandle scrollB)
  230. {
  231. MoveControl(scrollB, PRCT_R(wind) - kScrollbarAdjust, PRCT_T(wind) - 1);
  232. SizeControl(scrollB, kScrollbarWidth, PRCT_B(wind) - PRCT_T(wind) - 13);
  233. }
  234.  
  235. /* SetupHorzScrollBar: adjusts the horizontal scrollbar to the bottom border
  236. of the window */
  237.  
  238. void SetupHorzScrollBar(WindowPtr wind, ControlHandle scrollB)
  239. {
  240. MoveControl(scrollB, PRCT_L(wind) - 1, PRCT_B(wind) - kScrollbarAdjust);
  241. SizeControl(scrollB, PRCT_R(wind) - PRCT_L(wind) - 13, kScrollbarWidth);
  242. }
  243.  
  244. /* myStdFilterProc: standard filter proc for all alerts & simple dialogs
  245. WITH a cancel button */
  246.  
  247. pascal Boolean myStdFilterProc(DialogPtr theD, EventRecord *thEv, short *iHit)
  248. {
  249. GrafPtr    savePort;
  250. register Boolean    retVal;
  251.  
  252. switch (thEv->what) {
  253.     case updateEvt:
  254.         if (theD != (DialogPtr)thEv->message) {
  255.             DoUpdate(thEv);
  256.             *iHit = kfakeUpdateItem;
  257.             return false;
  258.             }
  259.         break;
  260.     case activateEvt:
  261.         if (thEv->modifiers & activeFlag) {
  262.             (void)SetDialogDefaultItem(theD, ((DialogPeek)theD)->aDefItem);
  263.             (void)SetDialogCancelItem(theD, cancel);
  264.             (void)SetDialogTracksCursor(theD, false);
  265.             }
  266.         if (theD != (DialogPtr)thEv->message) {
  267.             DoActivate(thEv);
  268.             *iHit = kfakeUpdateItem;
  269.             return false;
  270.             }
  271.         break;
  272.     }
  273. GetPort(&savePort);
  274. SetPort(theD);
  275. retVal = StdFilterProc(theD, thEv, iHit);
  276. SetPort(savePort);
  277. return retVal;
  278. }
  279.  
  280. /* myStdFilterProcNoCancel: standard filter proc for all alerts & simple dialogs
  281. WITHOUT a cancel button */
  282.  
  283. pascal Boolean myStdFilterProcNoCancel(DialogPtr theD, EventRecord *thEv, short *iHit)
  284. {
  285. GrafPtr    savePort;
  286. register Boolean    retVal;
  287.  
  288. switch (thEv->what) {
  289.     case updateEvt:
  290.         if (theD != (DialogPtr)thEv->message) {
  291.             DoUpdate(thEv);
  292.             *iHit = kfakeUpdateItem;
  293.             return false;
  294.             }
  295.         break;
  296.     case activateEvt:
  297.         if (thEv->modifiers & activeFlag) {
  298.             (void)SetDialogDefaultItem(theD, ((DialogPeek)theD)->aDefItem);
  299.             (void)SetDialogTracksCursor(theD, false);
  300.             }
  301.         if (theD != (DialogPtr)thEv->message) {
  302.             DoActivate(thEv);
  303.             *iHit = kfakeUpdateItem;
  304.             return false;
  305.             }
  306.         break;
  307.     }
  308. GetPort(&savePort);
  309. SetPort(theD);
  310. retVal = StdFilterProc(theD, thEv, iHit);
  311. SetPort(savePort);
  312. return(retVal);
  313. }
  314.  
  315. /* general error alert displaying error code */
  316.  
  317. void ErrorAlert(OSErr reason)
  318. {
  319. Str255    tempS;
  320.  
  321. InitCursor();
  322. MyNumToString(reason, tempS);
  323. ParamText(tempS, nil, nil, nil);
  324. (void)StopAlert_UPP(kALRT_ERROR, myStdFilterProcNoCancel);
  325. }
  326.  
  327. #pragma segment Rare
  328.  
  329. /* ResetRegisters: sets the main registers to their default values */
  330.  
  331. void ResetRegisters(void)
  332. {
  333. gRegs[kREG_PC] = gPrefs.DefPCValue;
  334. gRegs[kREG_SP] = gPrefs.DefSPValue;
  335. gParts[kP_MPC - kFIRST_PICT] = 0;
  336. }
  337.  
  338. /* ResetMemory: sets some important "System" memory locations */
  339.  
  340. void ResetMemory(void)
  341. {
  342. register Handle IllHandler = Get1Resource(krHandlers, kIllegalHandlerCode);
  343.  
  344. ((short *)gMMemory)[gPrefs.DefSPValue] = kPCExitToShell;
  345. ((short *)gMMemory)[kIllegalVector] = kIllegalHandler;
  346. BlockMoveData(*IllHandler, (Ptr)((short *)gMMemory + kIllegalHandler),
  347.             InlineGetHandleSize(IllHandler));
  348. }
  349.  
  350. #pragma segment __%Main
  351.  
  352. #endif
  353.  
  354.